home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Deutsche Edition 1
/
Deutsche Edition 1.iso
/
amok
/
amok_lha
/
amok15.lha
/
Seafarers_Manual
/
Source
/
RaisePower2.mod
< prev
next >
Wrap
Text File
|
1993-08-15
|
1KB
|
61 lines
MODULE RaisePower2; (* Raise x to y power (y>=0) using local variables *)
(* From the book "Modula-2 A Seafarer's Manual and Shipyard Guide" *)
(* Page 102 adapted "Amiga M2Modula-2" 08 Mar 1988 *)
FROM InOut IMPORT WriteLn,
WriteString,
ReadCard;
FROM RealInOut IMPORT WriteReal,
ReadReal;
VAR
z, (* accumulates result *)
x : REAL; (* base from keyboard *)
y : CARDINAL; (* exponent from keyboard *)
PROCEDURE Getxy; (* get x & y from keyboard *)
BEGIN
WriteLn;
WriteString ("Enter x: ");
ReadReal (x);
WriteLn;
WriteString ("Enter y: ");
ReadCard (y);
END Getxy;
PROCEDURE Power; (* raise x to y power *)
VAR
e : CARDINAL; (* holds exponent *)
t : REAL; (* intermediate result *)
BEGIN
e := y; (* initialize exponent *)
t := x; (* initialize intermediate result *)
z := 1.0; (* initialize result *)
WHILE (e # 0) DO
WHILE (NOT ODD(e)) DO
t := t * t;
e := e DIV 2;
END; (* WHILE NOT *)
z := z * t;
DEC (e);
END; (* WHILE e *)
END Power;
PROCEDURE DisplayAnswer;
BEGIN
WriteLn;
WriteString ("x to y power = ");
WriteReal (z,10,2);
WriteLn;
END DisplayAnswer;
BEGIN (* MODULE RaisePower *)
WriteLn;
WriteString ("Calculating x to y power");
Getxy;
Power;
DisplayAnswer;
END RaisePower2.